/*
 * Copyright 2017, Data61, CSIRO (ABN 41 687 119 230)
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <platsupport/io.h>
#include <stdint.h>
#include <sys/types.h>
#include <utils/util.h>

/** \brief A convenience type for CAmkES dataports.
 *
 * The default dataport type in CAmkES is `Buf`, a generic type for a
 * page-sized shared memory region. While the user is provided with a typed
 * pointer, this should never be directly dereferenced. The only legal things
 * to do with a `Buf` are to call `sizeof` or cast a pointer to one to a
 * pointer of another type. E.g.
 *
 *     char my_buffer[sizeof(Buf)];
 *     strncpy(my_char, buf_ptr, sizeof(Buf));
 */
typedef struct Buf_ {
    char content[PAGE_SIZE_4K] DEPRECATED("Buf type incorrectly accessed directly");
} Buf;

/* This type is intended to be used opaquely by a user. */
typedef struct dataport_ptr_ {
    int32_t id;
    off_t offset;
} dataport_ptr_t;

struct dataport_frame {
    uintptr_t paddr;
    uintptr_t cap;
    uintptr_t size;
    uintptr_t vaddr;
};
typedef struct dataport_frame dataport_frame_t;

/* These functions are generated by the CAmkES platform compilation, but are
 * intended to be invoked by the user.
 */
dataport_ptr_t dataport_wrap_ptr(void *ptr);
void *dataport_unwrap_ptr(dataport_ptr_t ptr);

int camkes_dataport_flush_cache(size_t start_offset, size_t size,
                                uintptr_t dataport_start, size_t dataport_size,
                                dma_cache_op_t cache_op);
